﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Connector = TransactionServer.General_Classes;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;

namespace TransactionServer.CRMActivities
{

    public sealed class CRMGetEntityById : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<Connector.Context> Context { get; set; }
        public InArgument<string[]> Attributes { get; set; }
        public InArgument<String> EntityName { get; set; }
        public InArgument<String> EntityId { get; set; }
        public InArgument<String> SystemName { get; set; }

        public OutArgument<Connector.Entity> Target { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string[] attributes = context.GetValue(this.Attributes);
            string entityName = context.GetValue(this.EntityName);
            string entityId = context.GetValue(this.EntityId);
            string systemName = context.GetValue(this.SystemName);
            var con = context.GetValue(this.Context);
            CRMConnector conn = (CRMConnector)con.Connector[systemName];

            context.SetValue(this.Target, conn.getEntityById(systemName, entityName, entityId, attributes));
        }
    }
}
